package com.yex.util;

import com.yex.common.exception.RRException;
import com.yex.entity.UserEntity;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class CommonModifyUtil {

    public static HttpServletRequest getRequest() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return servletRequestAttributes.getRequest();
    }

    public static String getOrderId() {
        SimpleDateFormat sfDate = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String strDate = sfDate.format(new Date());
        System.out.println("时间17位：" + strDate);
        String result = "";
        Random rand = new Random();
        int n = 5;
        int randInt;
        for (int i = 0; i < n; i++) {
            randInt = rand.nextInt(10);
            result += randInt;
        }
        return strDate + result;
    }

    public static void checkPayPassword(String moneyPassword, UserEntity userEntity) {
        if (StringUtils.isBlank(moneyPassword)) throw new RRException("请输入支付密码");
        if (StringUtils.isBlank(userEntity.getMoneyPassword())) throw new RRException("请设置支付密码");
        if (!DigestUtils.sha256Hex(moneyPassword).equalsIgnoreCase(userEntity.getMoneyPassword()))
            throw new RRException("支付密码错误!");
    }


}
